package cn.xny.org.service.impl;

import cn.xny.basic.service.impl.BaseServiceImpl;
import cn.xny.org.domain.Department;
import cn.xny.org.mapper.DepartmentMapper;
import cn.xny.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

  @Autowired
  private DepartmentMapper departmentMapper;



    @Override
    public List<Department> deptTree() {

        List<Department> departments = departmentMapper.loadAll();

        Map<Long,Department> Map = new HashMap<>();


        for (Department department : departments) {
            Map.put(department.getId(),department);

        }
        List<Department> deptTree = new ArrayList<>();

        for (Department department : departments) {
              if(department.getParent_id()==null){

                  deptTree.add(department);
              }else {
                  Long parentId = department.getParent_id();
                  Department department1 = Map.get(parentId);
                  department1.getChildren().add(department);

              }
        }

               return deptTree;

    }


}
